library(tidyverse)
library(gapminder)
library(here)
Figure 3.4: A scatterplot of life expectancy vs GDP
p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap,y =lifeExp))
p + geom_point()
Figure 3.5: Life expectancy vs GDP, using a GAM smoother
p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap,y = lifeExp))
p + geom_smooth()
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
Figure 3.6: Life expectancy vs GDP, using a GAM smoother and points
p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap,y = lifeExp))
p + geom_smooth() + geom_point()
Figure 3.7: Life expectancy vs GDP, using a linear model and points
p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap,y = lifeExp))
p + geom_point() + geom_smooth(method = "lm")
Figure 3.8: Life expectancy vs GDP scatterplot, with a GAM smoother and a log scale on the x-axis
p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap,y = lifeExp))
p + geom_point() + geom_smooth(method = "gam") + scale_x_log10()
Figure 3.9: Life expectancy vs GDP scatterplot, with a GAM smoother and a log scale on the x-axis, with x-labels as dollar values
p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap,y = lifeExp))
p + geom_point() + geom_smooth(method = "gam") + scale_x_log10(labels = scales::dollar)
ggplot(data = gapminder,
mapping = aes(x = gdpPercap,y =lifeExp,color = continent)) +
geom_point() + scale_x_log10(labels = scales::dollar)
Figure 3.10: Same as above, but setting color independently of dataset variables.
p <- ggplot(data = gapminder,
mapping = aes(x = gdpPercap, y = lifeExp,
color = c("purple")))
p + geom_point() + geom_smooth(method = "loess") + scale_x_log10()
An aesthetic is a mapping of variables in your data to properties you can see on the graph. The aes() function is where the mappings are specified: where you can translate variables to graphical properties.
The code for section 3.10, in effect, creates a new variable for the data. The string “purple” is recycled for every row.
The aes() function is for mappings only. Do not use it to change properties to a particular value. If we want to set a property, we should do within each specific geom_ and outside the mapping = aes().
Figure 3.11: Setting the color attribute of the points directly
p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap,
y = lifeExp))
p + geom_point(color = "purple") + geom_smooth(method = "loess") +
scale_x_log10(labels = scales::dollar)
Figure 3.12: Changing other arguments
p <- ggplot(gapminder,aes(x = gdpPercap, y = lifeExp))
p + geom_point(alpha = 0.3) + geom_smooth(color = "orange", se = FALSE,
size =1.5, method = "lm") +
scale_x_log10()
Figure 3.13: A more polished plt of Life Expectancy vs GDP
p <- ggplot(gapminder, aes(x = gdpPercap, y = lifeExp))
p + geom_point(alpha = 0.3) + ###Setting transparency of points
geom_smooth(method = "gam") + ##Setting method
scale_x_log10(labels = scales::dollar) +
labs(x = "GDP Per Capita in U$",
y = "Life Expectancy in Years",
title = "Economic Growth and Life Expectancy",
subtitle = "Data points are country years",
caption = "Source: Gapminder")
The labs() function controls the main labels of scales. Axis tick-marks is the responsability of the various scale_ functions.
Unless told otherwise, all geoms layered on top of the original plot object will inherit that object’s mapping.
Figure 3.14: Mapping the continent variable to the color aesthetic
p <- ggplot(data = gapminder,
mapping = aes(x = gdpPercap,
y = lifeExp,
color = continent))
p + geom_point() + geom_smooth(method = "loess") + scale_x_log10() +
theme(legend.position = "top")
Figure 3.15: Mapping the continent variable to the color and fill aesthetics
p <- ggplot(gapminder, aes(x = gdpPercap,
y = lifeExp,
color = continent,
fill = continent))
p + geom_point() + geom_smooth(method = "loess") + scale_x_log10()
By default, geoms inherit their mappings from the ggplot() function. We can change this by specifying different aesthetics for each geom(). We can use the same mapping = aes(…) expression as in the initial call to ggplot(), but now use it in the geom_ functions as well, specifying the mappings we want to apply to each one.
Mappings specified in the initial ggplot() function will carry through to all subsequent geoms.
Figure 3.16: Mapping aesthetics on a per geom basis. Color is mapped to continent for the points, but not for the smoother.
p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap,
y = lifeExp))
p + geom_point(mapping = aes(color= continent)) +
geom_smooth(method = "loess") +
scale_x_log10()
Figure 3.17; Mapping a continuous variable to color
p <- ggplot(data = gapminder,
mapping = aes(x = gdpPercap, y = lifeExp))
p + geom_point(mapping = aes(color = log(pop))) + scale_x_log10()
Figure 3.x1: Setting the size of output for each R markdown chunk
p <- ggplot(data = gapminder,
mapping = aes(x = gdpPercap, y = lifeExp))
p + geom_point(mapping = aes(color = log(pop))) + scale_x_log10()